package com.sport.ui.tool;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.sport.databinding.FragmentToolBodyBinding;


public class BodyFragment extends Fragment {
    private FragmentToolBodyBinding binding;
    private TextView editBtn, totalTime;
    private Button btnClock;
    private boolean isTimerRunning = false;
    //记录时长
    private int seconds = 0;
    private int hours, minutes, remaining = 0;
    private Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentToolBodyBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        init();
        return view;
    }
    public void init() {
        editBtn = binding.editBtn;
        editBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //滚动选择器
                final NumberPicker dayPicker = new NumberPicker(requireContext());
                dayPicker.setMaxValue(7);
                dayPicker.setMinValue(1);
                dayPicker.setValue(1);

                final NumberPicker minutePicker = new NumberPicker(requireContext());
                minutePicker.setMaxValue(180);
                minutePicker.setMinValue(0);
                minutePicker.setValue(30);

                LinearLayout layout = new LinearLayout(requireContext());
                layout.setOrientation(LinearLayout.HORIZONTAL);
                layout.addView(dayPicker);
                layout.addView(minutePicker);

                AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
                builder.setTitle("选择健身计划");
                builder.setMessage("目标天数和每天锻炼时间");
                builder.setView(layout);

                // 添加“确定”按钮
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String goalText = String.format("目标 %d 天，每天锻炼 %d 分钟", dayPicker.getValue(), minutePicker.getValue());
                        binding.weeklyGoalDetail.setText(goalText);
                    }
                });

                // 添加“取消”按钮
                builder.setNegativeButton("取消", null);

                // 显示对话框
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
        totalTime = binding.totalTimeValue;
        btnClock = binding.btnClock;
        convertTime(totalTime.getText().toString().trim());
        btnClock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isTimerRunning) {
                    startTimer();
                    btnClock.setText("Stop recording");
                } else {
                    stopTimer();
                    btnClock.setText("Begin your plan NOW!");
                }
            }
        });
    }
    private void startTimer() {
        isTimerRunning = true;
        totalTime.setText("00:00");
        // 记录启动计时器的时间
        handler = new Handler();
        //postDelayed 方法每一秒更新一次时间
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                seconds++;
                int hours = seconds / 3600;
                int minutes = seconds / 60;
                int remaining = seconds % 60;

                String time;
                if (hours > 0) {
                    time = String.format("%d:%02d:%02d", hours, minutes, remaining);
                } else {
                    time = String.format("%02d:%02d", minutes, remaining);
                }
                totalTime.setText(time);
                if (isTimerRunning) {
                    handler.postDelayed(this, 1000);
                }
            }
        }, 1000);
    }

    private void stopTimer() {
        isTimerRunning = false;
        handler.removeCallbacksAndMessages(null); // 停止计时器

        // 更新已累计时间
        int hours = seconds / 3600;
        int minutes = seconds / 60;
        int remaining = seconds % 60;
        this.hours += hours;
        this.minutes += minutes;
        this.remaining += remaining;
        String time;
        if (this.hours > 0) {
            time = String.format("%d:%02d:%02d", this.hours, this.minutes, this.remaining);
        } else {
            time = String.format("%02d:%02d", this.minutes, this.remaining);
        }
        totalTime.setText(time);
        seconds = 0;
    }

    private void convertTime(String time) {
        String[] parts = time.split(":");
        this.hours = Integer.parseInt(parts[0]);
        this.minutes = Integer.parseInt(parts[1]);
        this.seconds = Integer.parseInt(parts[2]);
    }

}